using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.UI;

public class PassPanel : BasePanel, IInitializable
{
    // public GameObject arrowObject;
    // public GameObject arrowObjectContainer;
    public ArrowMover arrowMover;
    private int currentIndex = 1;
    private List<int> bonusList = new List<int> { 2, 3, 5, 3, 2 };
    public Text claimx3;
    public Text claimx3Value;
    public void Initialize(object data)
    {
        Debug.Log("PassPanel initialized" + data);
    }
    public void OnCloseClick()
    {
        // 关闭面板
        PanelManager.Instance.ClosePanel<PassPanel>();
    }

    public override void OnOpen()
    {
        base.OnOpen(); // 调用基类的 OnOpen 方法
        GlobalEventManager.OnEvent("AdsPlaybackCompleted", OnAdsPlaybackCompleted);

        // 在面板打开时执行的逻辑
        Debug.Log("PassPanel opened");
        AudioManager.Instance.PlaySoundEffect("sounds_win");

        // GameObject prefab = Instantiate(arrowObject);
        // prefab.transform.SetParent(arrowObjectContainer.transform);
        // prefab.transform.position = arrowObjectContainer.transform.position;
        // arrowMover = prefab.transform.Find("Arrow").GetComponent<ArrowMover>();

        //BI DOTEVENT_START
        int _level = -1;
        if (Data.mode == 0)
        {
            _level = (int)UserManager.Instance.GetUserInfo()["CurrentLv"];

        }
        else if (Data.mode == 1)
        {
            _level = (int)UserManager.Instance.GetUserInfo()["LimitedLv"];
        }
        var grs = new Dictionary<string, object>
            {
                { "level", _level }
            };

        string ghb = JsonConvert.SerializeObject(grs);
        IOSHelper.SendDataEyeLog("level_passed", ghb);
        //BI DOTEVENT_END
    }

    void Update()
    {
        if (arrowMover != null)
        {
            int currentBlock = arrowMover.GetCurrentBlockIndex();
            currentIndex = currentBlock;
            Debug.Log("Arrow is currently under block: " + bonusList[currentBlock]);
            claimx3.text = "Claim x" + bonusList[currentIndex];
            claimx3Value.text = 100 * bonusList[currentIndex] + "";
        }

    }

    public override void OnClose()
    {
        base.OnClose(); // 调用基类的 OnClose 方法
        GlobalEventManager.OffEvent("AdsPlaybackCompleted", OnAdsPlaybackCompleted);

        // 在面板关闭时执行的逻辑
        Debug.Log("PassPanel closed");
    }

    public void OnClaimClick()
    {
        // 领取奖励
        Debug.Log("Claim reward");
        UserManager.Instance.AddCoins(100f);
        OnCloseClick();
        if (Data.mode == 0)
        {
            UserManager.Instance.LevelUp();
        }
        else if (Data.mode == 1)
        {
            UserManager.Instance.LevelUpLimitedLv();
        }
        GlobalEventManager.EmitEvent("NextLevel");
    }

    public void OnWatchAdsClick()
    {
        // 点击了看广告按钮
        Debug.Log("WatchAds button clicked");
        bool st = AdsManager.Instance.IsRewardedAdReady();
        if (st)
        {
            AdsManager.Instance.ShowRewardedAd();
        }
        else
        {
            Tools.ShowTipToast("Ad is not Ready");
        }
    }

    public void OnAdsPlaybackCompleted()
    {
        Debug.Log("广告播放完成");

        UserManager.Instance.AddCoins(100f * bonusList[currentIndex]);
        OnCloseClick();
        if (Data.mode == 0)
        {
            UserManager.Instance.LevelUp();
        }
        else if (Data.mode == 1)
        {
            UserManager.Instance.LevelUpLimitedLv();
        }
        GlobalEventManager.EmitEvent("NextLevel");
    }
}
